/*
 * app_adc16.c
 *
 *  Created on: Oct 8, 2013
 *      Author: Omar Sevilla
 */
#include "derivative.h"
#include "stdtypedef.h"
#include "drv_adc8.h"

void init_adc(void){
	//Habiliar Reloj para ADC0
	SIM_SCGC6 |= SIM_SCGC6_ADC0_MASK;
	//ADC TRIGGER
	SIM_SOPT7 = 0x00;
	
	//Set Clock Ports
	SIM_SCGC5 |= SIM_SCGC5_PORTE_MASK;
	
	PORTE_PCR20 = PORT_PCR_MUX(0x00); //Set PTE20 as ADC Pin ADC0_SE0(Alternative 0)
	PORTE_PCR21 = PORT_PCR_MUX(0x00); //Set PTD21 as ADC Pin ADC0_SE4(Alternative 0)
	PORTE_PCR22 = PORT_PCR_MUX(0x00); //Set PTD22 as ADC Pin ADC0_SE3(Alternative 0)
	
	//ADC configuration
	ADC0_CFG1 = 0x31;	//MODE 16 bits
	ADC0_CFG2 = 0x00;
	ADC0_SC2 = 0x00;	//Triggered By Software (Writting SCA1)
	ADC0_SC3 = 0x04;
}

T_UWORD get_adc(T_UWORD T_UWORD_ADCChannel){
	ADC0_SC1A = /*(T_UWORD_ADCAux |= */T_UWORD_ADCChannel;
	while((ADC0_SC1A & (0x80)) == 0){
		//Wait conversion
	}
	return(ADC0_RA);
}

